# The American Viewer: Political Consequences of Entertainment Media
# Eunji Kim and Shawn Patterson Jr 
# American Political Science Review 
# Replication File [Figure 2]


# Libraries
library(lubridate)
library(readr)
library(tidyverse)
library(scales)
library(MetBrewer)


data <- read.csv("figure2.csv")

data <- data  %>%  
  mutate(DateStart = substr(Dates, 1, 10) %>% as.Date(format="%m/%d/%Y"))

data$Issue <- factor(data$Issue, levels = c( "immigration", "health care",  "social security",  "apprentice"))

figure2 <- ggplot(data, aes(x = DateStart, y = N, fill = Issue)) + 
  geom_bar(stat = "identity", color="black", size=0.1) +
  scale_fill_manual(values = c("white", "grey66", "grey33", "#8a0002")) +
  scale_x_date(date_breaks = "months" , date_labels = "%b'%y", limits = c(as.Date("2015-06-16"), as.Date("2016-11-10")), expand = c(0.01, 0)) +  
  scale_y_continuous(labels=comma, breaks=c(seq(0, 2500, 500))) +  
  
  geom_vline(xintercept = as.Date("2016-07-06"), linetype="dotted", color = "black", size=0.5) +  
  labs(y = "Number of Articles") +  
  theme_classic() +  
  theme(axis.title.x = element_blank(),        
        axis.title.y = element_text(color="black", size=10, face="bold"),        
        axis.text.x = element_text(color= "black", size=7, face="bold"),        
        axis.text.y = element_text(color= "black", size=9, face="bold"),        
        legend.position = "bottom",     
        legend.title= element_blank(),
        plot.margin = margin(0.5, 0.5, 0.5, 0.25, "cm")) +
  annotate(geom="label", label = "Trump wins the\nRepublican Nomination", x=as.Date("2016-07-06"), y=2500, hjust=0.5, fontface=2, size=2)   



jpeg("Figure2.jpeg", width=9, height=5, units='in', res=1000)

figure2


dev.off()